package com.example.admin.brainwave.ui.activity;

import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.admin.brainwave.R;
import com.example.admin.brainwave.base.BaseActivity;
import com.example.admin.brainwave.bean.CurrencyBean;
import com.example.admin.brainwave.net.GetNoteTask;
import com.example.admin.brainwave.net.ResetPassTask;
import com.example.admin.brainwave.utils.ActivityUtil;
import com.example.admin.brainwave.utils.Const;
import com.example.admin.brainwave.utils.MyCountDownTimer;
import com.example.admin.brainwave.utils.ToastUtil;

import butterknife.BindView;

public class ResetPassActivity extends BaseActivity {


    @BindView(R.id.iv_top_back)
    ImageView ivTopBack;
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.iv_reset_phone)
    ImageView ivResetPhone;
    @BindView(R.id.et_reset_phone)
    EditText etResetPhone;
    @BindView(R.id.ll_reset_phone)
    LinearLayout llResetPhone;
    @BindView(R.id.iv_reset_validate)
    ImageView ivResetValidate;
    @BindView(R.id.et_reset_validate)
    EditText etResetValidate;
    @BindView(R.id.btn_reset_validate)
    Button btnResetValidate;
    @BindView(R.id.ll_reset_validate)
    LinearLayout llResetValidate;
    @BindView(R.id.iv_reset_newpass)
    ImageView ivResetNewpass;
    @BindView(R.id.et_reset_newpass)
    EditText etResetNewpass;
    @BindView(R.id.ll_reset_newpass)
    LinearLayout llResetNewpass;
    @BindView(R.id.btn_reset_reset)
    Button btnResetReset;
    private String phone;
    private String newpass;
    private String code;
    MyCountDownTimer myCountDownTimer;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_reset_pass;
    }


    @Override
    protected void initView() {
        tvTopTitle.setText(getResources().getString(R.string.reset_password));
    }


    @Override
    protected void initData() {
        myCountDownTimer = new MyCountDownTimer(60000, 1000, btnResetValidate, this);
    }

    @Override
    protected void addListener() {
        ivTopBack.setOnClickListener(view -> finish());
        btnResetValidate.setOnClickListener(view -> {
            phone = etResetPhone.getText().toString();
            if (!TextUtils.isEmpty(phone) && phone.length() == 11) {
                myCountDownTimer.start();
                getValidate();
            }
        });
        btnResetReset.setOnClickListener(view -> {
            if (isEmpty()) {
                resetPass();
            }
        });
    }


    private void getValidate() {
        new GetNoteTask(this, o -> {
            if (o != null) {
                CurrencyBean c = (CurrencyBean) o;
                ToastUtil.showShort(ResetPassActivity.this, c.getMessage());
            }
        }).execute(phone);
    }


    private void resetPass() {
        new ResetPassTask(this, o -> {
            if (o != null) {
                CurrencyBean c = (CurrencyBean) o;
                if (Const.OKCODE.equals(c.getCode())) {
                    ToastUtil.showShort(ResetPassActivity.this, c.getMessage());
                    ActivityUtil.finishAllActivity();
                    ActivityUtil.startActivity(ResetPassActivity.this, LoginForPassActivity.class);
                } else {
                    ToastUtil.showShort(ResetPassActivity.this, c.getMessage());
                }
            }
        }).execute(phone, code, newpass);
    }

    private boolean isEmpty() {
        phone = etResetPhone.getText().toString();
        newpass = etResetNewpass.getText().toString();
        code = etResetValidate.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            ToastUtil.showShort(this, getResources().getString(R.string.phone) + " " + getResources().getString(R.string.not_null));
            return false;
        }
        if (TextUtils.isEmpty(newpass)) {
            ToastUtil.showShort(this, getResources().getString(R.string.password) + " " + getResources().getString(R.string.not_null));
            return false;
        }
        if (newpass.length() < 8) {
            ToastUtil.showShort(this, getResources().getString(R.string.password) + " " + getResources().getString(R.string.length_too_short));
            return false;
        }
        if (TextUtils.isEmpty(code)) {
            ToastUtil.showShort(this, getResources().getString(R.string.verification_code) + " " + getResources().getString(R.string.not_null));
            return false;
        }
        return true;
    }
}
